package com.example.chepai_test.car.sb;

import com.example.chepai_test.car.auth.AuthServiceChePai;
import com.example.chepai_test.car.util.Base64Util;
import com.example.chepai_test.car.util.FileUtil;
import com.example.chepai_test.car.util.HttpUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.Map;

public class ChePaiShiBie {
    public static String ChePaiShiBie() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
        try {
            // 本地文件路径
            String filePath = "C:\\Users\\86150\\Desktop\\car2.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam+"&multi_detect="+"true"+"&multi_scale="+"true";

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthServiceChePai.getAuth();

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String result = ChePaiShiBie.ChePaiShiBie();
        JSONObject jsonobj = new JSONObject(result);
        JSONArray words_result = jsonobj.getJSONArray("words_result");
        System.out.println(words_result.length());
        for (int i = 0; i < words_result.length(); i++) {
           JSONObject sigcp = words_result.getJSONObject(i);
            System.out.println(sigcp.getString("number"));
        }

    }
}
